<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of qcm
 *
 * @author didile
 */
class Qcm {

    public $id;
    public $qcmQuestionsArray;
    public $nbQuestion;
    public $libelle;
     
	public function __construct($id='') {
            
                $this->qcmQuestionsArray = array();
                
                if($id != ''){
			$this->id = $id;
			$this->load();
		}
		/*
                             * SELECT q.idQuestion
                                FROM rubrique as r, question as q, rubrique_question as rq
                                WHERE r.idRubrique = rq.idRubrique
                                AND q.idQuestion = rq.idQuestion
                                AND rq.idRubrique = 19
                 * 
                 */
	}
        
        public function load() {
		if(isset($this->id)){
			$db = Database::getInstance();
			$sql= 'SELECT q.idQuestion,r.libelle
                                FROM rubrique as r, question as q, rubrique_question as rq
                                WHERE r.idRubrique = rq.idRubrique
                                AND q.idQuestion = rq.idQuestion
                                AND rq.idRubrique = '.$this->id.'';					
						  
					  
			if ($result = $db->fetch($sql)){	
                            foreach($result as $row){ 
                                array_push($this->qcmQuestionsArray, new QuestionQCM($row['idQuestion'])); 
                            }				
                            
                            $this->nbQuestion = count($this->qcmQuestionsArray);
                            $this->libelle = $row['libelle'];
                            return true;
			}
			return false;	
		}
	}
	
	public function __toString(){
            return " ".$this->libelle." ".$this->nbQuestion;
	}
        
       	
        public function getNbQuestion() {           
            return $this->nbQuestion;            
        }
        
        public function joinRubriqueQuestion($idRubrique,$idQuestion){
            $db = Database::getInstance();
		$sql= 'INSERT INTO `rubrique_question` (
		`idRubrique` ,
		`idQuestion` ,
                `date`
		)
		VALUES 
		(
		"'.$idRubrique.'",	
		"'.$idQuestion.'",		
		CURRENT_TIMESTAMP)';
		
		if($db->exec($sql)){
			return true;
		}
		
		return false;
		
        }
        
        public function joinQuestionProposition($idQuestion,$idProposition){
            $db = Database::getInstance();
		$sql= 'INSERT INTO `question_proposition` (
		`idQuestion` ,
		`idProposition` ,
                `date`
		)
		VALUES 
		(
		"'.$idQuestion.'",	
		"'.$idProposition.'",		
		CURRENT_TIMESTAMP)';
		
		if($db->exec($sql)){
			return true;
		}
		
		return false;
        }
        
        public function joinQuestionReponse($idQuestion,$idReponse){
             $db = Database::getInstance();
		$sql= 'INSERT INTO `question_reponse` (
		`idQuestion` ,
		`idReponse` ,
                `date`
		)
		VALUES 
		(
		"'.$idQuestion.'",	
		"'.$idReponse.'",		
		CURRENT_TIMESTAMP)';
		
		if($db->exec($sql)){
			return true;                        
		}
		
		return false;
        }

        public function addQuestionToQCM($idRubrique,$libelleQuestion,$libelleProp1,$libelleProp2,$libelleProp3,$description,$numRep){
            
            $estReponse1 = false;
            $estReponse2 = false;
            $estReponse3 = false;
            
            $question = new Question();
            $idQuestion = $question->addQuestion($libelleQuestion);
            $this->joinRubriqueQuestion($idRubrique, $idQuestion);
            
            $reponse = new Reponse();
            $idReponse = $reponse->addReponse($description);
            $this->joinQuestionReponse($idQuestion, $idReponse);
            
            switch ($numRep) {
	
                case 1 :
                         $estReponse1 = true;
                        break;

                case 2 :
                        $estReponse2 = true;
                        break;

                case 3 :
                         $estReponse3 = true;
                        break;	
            }
            
            $proposition1 = new Proposition();
            $idProposition1 =  $proposition1->addProposition($libelleProp1, $estReponse1);
            $this->joinQuestionProposition($idQuestion, $idProposition1);
            
            $proposition2 = new Proposition();
            $idProposition2 =  $proposition2->addProposition($libelleProp2, $estReponse2);
            $this->joinQuestionProposition($idQuestion, $idProposition2);
            
            $proposition3 = new Proposition();
            $idProposition3 =  $proposition3->addProposition($libelleProp3, $estReponse3);
            $this->joinQuestionProposition($idQuestion, $idProposition3);
        }
        
       
    //put your code here
}

?>
